var UIController = require("UIController");
var PlayerController = require("PlayerController");
const Weapon = require("../Weapons/Weapon");
var PlayerStatController = require("PlayerStatController");
var SFXManager = require("SFXManager");

var ExperienceLevelController = cc.Class({
    extends: cc.Component,

    properties: {
        currentExperience: 0,  

        expPrefab: cc.Prefab,

        expLevels: [cc.Integer],
        currentLevel: 1,
        levelCount: 10,

        weaponToUpgrade: [Weapon],
    },

    statics: {
        instance: null,
    },
    ctor () {
        ExperienceLevelController.instance = this;
    },

    // LIFE-CYCLE CALLBACKS:

    start () {
        this.currentExperience = 0;
        while (this.expLevels.length < this.levelCount) {
            this.expLevels.push(Math.floor(this.expLevels[this.expLevels.length - 1] * 1.1));
        }
    },

    getExp (amountToExp) {
        this.currentExperience += amountToExp;

        if (this.currentExperience >= this.expLevels[this.currentLevel]) {
            this.levelUp();
        }

        UIController.instance.updateExp(this.currentExperience, this.expLevels[this.currentLevel], this.currentLevel);

        SFXManager.instance.playSFX(1);
    },

    spawnExp (position) {
        let prefab = cc.instantiate(this.expPrefab);
        prefab.setPosition(position);

        return prefab;
    },

    levelUp () {
        this.currentExperience -= this.expLevels[this.currentLevel];

        this.currentLevel++;

        if (this.currentLevel >= this.expLevels.length) {
            this.currentLevel = this.expLevels.length - 1;
        }

        //PlayerController.instance.activeWeapon.levelUp();
        UIController.instance.levelUpPanel.active = true;

        cc.director.pause("Main");

        this.weaponToUpgrade = [];
        let availableWeapons = [];
        availableWeapons = availableWeapons.concat(PlayerController.instance.assignedWeapons);

        if (availableWeapons.length > 0) {
            let selected = Math.floor(Math.random() * availableWeapons.length);
            this.weaponToUpgrade.push(availableWeapons[selected]);
            availableWeapons.splice(selected, 1);
        }

        if (PlayerController.instance.assignedWeapons.length + PlayerController.instance.fullLevelWeapons.length < PlayerController.instance.maxWeaponNumber) {
            availableWeapons = availableWeapons.concat(PlayerController.instance.unassignedWeapons);
        }

        for (let i = this.weaponToUpgrade.length; i < 3; i++) {
            if (availableWeapons.length > 0) {
                let selected = Math.floor(Math.random() * availableWeapons.length);
                this.weaponToUpgrade.push(availableWeapons[selected]);
                availableWeapons.splice(selected, 1);
            }
        }

        for (let i = 0; i < this.weaponToUpgrade.length; i++) {
            UIController.instance.levelUpButtons[i].updateButtonDisplay(this.weaponToUpgrade[i]);
        }

		for (let i = 0; i < UIController.instance.levelUpButtons.length; i++) {
			let button = UIController.instance.levelUpButtons[i].node;
			if (i >= this.weaponToUpgrade.length) {
				button.active = false;
			} else {
                button.active = true;
            }
		}

        PlayerStatController.instance.updateDisplay();
    },
});

module.exports = ExperienceLevelController;
